export default {
  name: "contract-approval-manage",
  data() {
    return {
      isActive: 0,
      isShow: true,
      focuses: [
        { focus: "approvalAll" }, //全部
        { focus: "approvalPending" }, //待审批
        { focus: "approvalPass" } //已审批
      ],
      // 非锁定表格
      unfixedTableThead: [
        {
          label: "审批类型",
          prop: "todoData.businessBillTypeName",
          width: "100"
        },
        {
          label: "入库单名称",
          prop: "businessData.storageName",
          width: "150"
        },
        {
          label: "供应商",
          prop: "businessData.supplierName",
          width: "200"
        },
        {
          label: "仓库",
          prop: "businessData.warehouseName",
          width: "150"
        },
        {
          label: "填报单位",
          prop: "businessData.reportInOrgName",
          width: "180"
        },
        {
          label: "入库类型",
          prop: "businessData.storageTypeName",
          width: "100"
        },
        {
          label: "编制人",
          prop: "businessData.reportUsername",
          width: "80"
        },
        {
          label: "编制日期",
          prop: "businessData.reportTime",
          width: "130"
        },
        {
          label: "审批状态",
          prop: "todoData.workItemStatusString",
          width: "100"
        }
      ],
      storageTypeList: [
        { label: "正式入库", value: "1" },
        { label: "暂估入库", value: "2" }
      ],
      storageStatusList: [
        { label: "待审批", value: "0" },
        { label: "已审批", value: "1" }
      ],
      approvalTypeList: [
        { label: "财务入库", value: "S0402" },
        { label: "财务暂估入库", value: "S0401" },
        { label: "财务入库冲销", value: "S0403" }
      ]
    };
  },
  methods: {
    // 默认渲染表格
    renderTable() {
      const localTableData = localStorage.getItem(this.tableName)
        ? JSON.parse(localStorage.getItem(this.tableName))
        : [];
      let unfixedTableThead = this.unfixedTableThead.concat(
        this.fixedTableThead
      );
      let localTableDataStr = localTableData.map(
        ({ label, prop, formatter, align }) =>
          JSON.stringify({ label, prop, formatter, align })
      );
      let unfixedTableTheadStr = unfixedTableThead.map(
        ({ label, prop, width, formatter, align }) =>
          JSON.stringify({ label, prop, formatter, align })
      );

      if (localTableData.length === unfixedTableThead.length) {
        let flag = localTableDataStr.every(item =>
          unfixedTableTheadStr.includes(item)
        );
        if (flag) {
          // 完全相同
          this.tableTheadOptions = localTableData;
          this.tableThead = localTableData;
          this.unfixedTableThead = localTableData.filter(item => {
            return !this.fixedTableThead.find(
              cItem => item.prop === cItem.prop
            );
          });
        } else {
          // 长度相同但值不同
          this.tableThead = this.unfixedTableThead.concat(this.fixedTableThead);
          const comment = JSON.stringify(this.tableThead);
          localStorage.setItem(this.tableName, comment);
          const localTableData = JSON.parse(
            localStorage.getItem(this.tableName)
          );
          this.tableTheadOptions = localTableData;
        }
      } else {
        // 长度不同
        this.tableThead = this.unfixedTableThead.concat(this.fixedTableThead);
        const comment = JSON.stringify(this.tableThead);
        localStorage.setItem(this.tableName, comment);
        const localTableData = JSON.parse(localStorage.getItem(this.tableName));
        this.tableTheadOptions = localTableData;
      }
    },
    // 页签跳转
    tabChange(data, index) {
      console.log("tab:", data);
      this.$router.push(`/inventoryManagement/financeApproval/${data}`);
    },
    // 详情
    handleGoDetail(row, index) {
      const { todoData, businessData } = row;
      const { businessBillTypeCode } = todoData;
      const { id } = businessData;
      const jumpUrl = {
        S0402: "/inventoryManagement/financeWarehousing/item-detail-approval/",
        S0401: "/inventoryManagement/financialList/item-detail-approval/",
        S0403: "/inventoryManagement/writeFinancialList/item-detail-approval/"
      };
      this.$router.push({
        path: `${jumpUrl[businessBillTypeCode]}${id}`,
        query: {
          type: "approval"
        }
      });
    },
    // 审核
    handleGoApproval(row) {
      const { todoData, businessData } = row;
      const { businessBillTypeCode } = todoData;
      this.$store.dispatch("setBusinessList", todoData);
      const businessList = JSON.stringify(todoData);
      sessionStorage.setItem("businessList", businessList);
      const { id, storageName: name } = businessData;
      const jumpUrl = {
        S0402: "/inventoryManagement/financeApproval/financeWarehousingDetail/",
        S0401: "/inventoryManagement/financeApproval/financialDetail/",
        S0403: "/inventoryManagement/financeApproval/writeFinancialDetaill/"
      };
      this.$router.push({
        path: `${jumpUrl[businessBillTypeCode]}${id}`,
        query: {
          name,
          type: "approval"
        }
      });
    }
  }
};
